package com.systemmanger.controller;

import com.bean.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.systemmanger.service.YongHuGuanLiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

//
@Controller
@RequestMapping("guanli")
public class YongHuGuanLiController {
    @Autowired
    YongHuGuanLiService YHLiService;

    /**
     * 到该页面和上下页的方法
     *
     * @param session
     * @param pageNum
     * @param pageSize
     * @param userName
     * @param userID
     * @return
     */
    @RequestMapping("User.do")
    public String page(HttpSession session,
                       @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                       @RequestParam(defaultValue = "15", value = "pageSize") Integer pageSize,
                       String userName, String userID) {

        PageInfo<WmsTUser> pageInfo = new PageInfo<>();
        if (pageNum == null || pageNum < 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        WmsTUser wmsTUser = new WmsTUser();
        wmsTUser.setUserName(userName);
        wmsTUser.setUserID(userID);
        session.setAttribute("userID", userID);
        session.setAttribute("userName", userName);
        //引入分页查询插件， pageNum是第几页，pageSize是每页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        List<WmsTUser> list = YHLiService.getUserMessage(wmsTUser);
        pageInfo = new PageInfo<WmsTUser>(list, pageSize);
        System.out.println(list);
        //System.out.println(pageInfo);
        //System.out.println(pageInfo.getList());
        session.setAttribute("pageInfo", pageInfo);
        return "guanli/UserManger";
    }

    @RequestMapping("User1.do")
    @ResponseBody
    public List<WmsTUser> page1(String userID) {
        WmsTUser wmsTUser = new WmsTUser();
        wmsTUser.setUserID(userID);
        System.out.println(wmsTUser);
        return YHLiService.getUserMessage(wmsTUser);
    }

    @RequestMapping("User2.do")
    @ResponseBody
    public List<WmsTGroup> select() {
        return YHLiService.sele();
    }

    @RequestMapping("User3.do")
    @ResponseBody
    public WmsTPermission returnPerName(String groupName) {
        return YHLiService.returnPerName(groupName);
    }

    @RequestMapping("User4.do")
    public String changeMessage(String userName, String userAccount, String userPwd, String userType,
                                String userNote, long state, HttpSession session, String userID, String groupName1) {

        WmsTUser wmsTUser = new WmsTUser();//修改user的数据，名字、账号、密码、用户类型、备注和修改时间
        WmsTPermGroup wmsTPermGroup = new WmsTPermGroup();//选择用户类型后，修改t_per_group里的permID,需要groupID
        WmsTUG wmsTUG = new WmsTUG();//选择组后，对应的用户类型改变-->t_u_g里的ugGroupID,需要ugUserID
        ObjectMapper ojbectMapper = new ObjectMapper();//bean类型转换
        if (state == 1) {
            //调用delete
            YHLiService.deleteUserMessage(userID);
        } else {
            wmsTUser.setUserID(userID);
            //查询到t_per_group的信息
            wmsTPermGroup.setGroupID(YHLiService.selectTUG(userID));
            // System.out.println(wmsTPermGroup.getGroupID());
            // System.out.println(userType);
            //修改permID
            if (userType.equals("管理员")) {
                wmsTPermGroup.setPermID("1");
                YHLiService.updatePerm(wmsTPermGroup);
            } else  {
                wmsTPermGroup.setPermID("2");
                YHLiService.updatePerm(wmsTPermGroup);
            }

            //修改t_u_g的信息
            System.out.println(groupName1);
            wmsTUG.setUgUserID(YHLiService.messageTUG(userID));
            wmsTUG.setUgGroupID(YHLiService.groupIDTGroup(groupName1));
            System.out.println(wmsTUG.getUgGroupID());
            YHLiService.updateTUG(wmsTUG);
            //日期更新
            LocalDate localDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String dateString = localDate.format(formatter);
            wmsTUser.setUserName(userName);
            wmsTUser.setUserAccount(userAccount);
            wmsTUser.setUserPwd(userPwd);
            wmsTUser.setUserType(userType);
            wmsTUser.setUserNote(userNote);
            wmsTUser.setUserEditTime(dateString);
            YHLiService.updateUser(wmsTUser);
        }
        PageInfo<WmsTUser> pageInfo = new PageInfo<>();
        wmsTUser.setUserID(userID);
        wmsTUser.setUserName(userName);
        List<WmsTUser> list = YHLiService.getUserMessage(wmsTUser);
        pageInfo = new PageInfo<WmsTUser>(list, 1);
        session.setAttribute("pageInfo", pageInfo);
        return "guanli/UserManger";
    }

}
